package com.cx.controller;

import com.cx.pojo.Club;
import com.cx.pojo.User;
import com.cx.service.ClubService;
import com.cx.utils.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/club")
public class ClubController {

    //    查询所有社团成员信息
    @RequestMapping("/getClubByIdList")
    public String getClubByIdList(Integer clubID, HttpServletRequest request, HttpSession session) {
        System.out.println("=========getClubByIdList======== ");

        List<User> list = clubService.getClubByIdList(clubID);
//        将集合保存到request
        request.setAttribute("list", list);
        return "clubByIdUserList";
    }
//=============================================================


    @Autowired
    private ClubService clubService;

    //    根据id查询指定的社团信息
    @RequestMapping("/getAllClubById")
    public String getAllClubById(Integer id, HttpServletRequest request) {
        System.out.println("=======getAllClubById========");
//        根据id查询指定的用户信息  将Club对象保存到request中
        request.setAttribute("ClubById", clubService.getAllClubById(id));
        return "clubUserList";
    }

    //根据社团id退出
    @RequestMapping("/getDeleteClubById")
    public String getDeleteClubById(Integer clubID, Integer id, HttpSession session) {
        System.out.println("========getDeleteClubById======");
        System.out.println("clubID = " + clubID + ", id = " + id);
//        执行删除
        clubService.getDeleteClubById(clubID, id);
        return "redirect:/club/clubHome";
    }

    //首页展示社团
    @RequestMapping("/clubHome")
    public String clubHome(@RequestParam(defaultValue = "1") Integer currentPage,
                             @RequestParam(defaultValue = "3") Integer pageSize,
                             HttpServletRequest request) {
        System.out.println("=========clubHome====================");
//        创建pages对象
        Pages clubHomePages = new Pages();
//        设置当前页
        clubHomePages.setCurrentPage(currentPage);
//        设置页面容量
        clubHomePages.setPageSize(pageSize);
//        设置总记录数
        clubHomePages.setTotalCount(clubService.count());
//        将clubHomePages对象保存到request中
        request.setAttribute("clubHomePages", clubHomePages);
//        分页查询
        List<Club> list = clubService.getAllClub(currentPage, pageSize);
//        将集合保存到request
        request.setAttribute("clubHomeList", list);

        return "clubHome";
    }

//    @RequestMapping("/clubHome")
//    public String clubHome(HttpServletRequest request) {
//        System.out.println("=========clubHome====================");
//        request.setAttribute("clubHomeList", clubService.getAllClub(1, clubService.count()));
//        return "clubHome";
//    }

    //    查询所有社团信息
    @RequestMapping("/getAllClub")
    public String getAllUser(@RequestParam(defaultValue = "1") Integer currentPage,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             HttpServletRequest request,
                             HttpSession session) {
        System.out.println("=========getAllClub====================");
        System.out.println("currentPage = " + currentPage);
//        创建pages对象
        Pages pages = new Pages();
//        设置当前页
        pages.setCurrentPage(currentPage);
//        设置页面容量
        pages.setPageSize(pageSize);
//        设置总记录数
        pages.setTotalCount(clubService.count());
//        将pages对象保存到request中
        request.setAttribute("ClubPages", pages);
//        分页查询
        List<Club> list = clubService.getAllClub(currentPage, pageSize);
//        将集合保存到request
        request.setAttribute("ClubList", list);

//        return "redirect:clubBackground";
        return "clubBackground";
    }

    //    根据id删除社团
    @RequestMapping("/deleteClubById")
    public String deleteClubById(Integer id) {
        System.out.println("========deleteClubById======");
//        执行删除
        clubService.deleteClubById(id);
        clubService.deleteClubTypeUseridById(id);
//        重定向请求
        return "redirect:/club/getAllClub";

    }

    //    根据id查询指定的社团信息
    @RequestMapping("/getClubById")
    public String getClubById(Integer id, HttpServletRequest request) {
        System.out.println("=======getClubById========");
//        根据id查询指定的用户信息  将Club对象保存到request中
        request.setAttribute("Club", clubService.getClubById(id));
        return "clubDetail";
    }

    @RequestMapping("/toEditClub")
    public String toEditClub(Integer id, HttpServletRequest request) {
        System.out.println("=======toEditClub========");
//        根据id查询指定的用户信息  将user对象保存到request中
        request.setAttribute("Club", clubService.getClubById(id));
        return "clubEdit";
    }

    @RequestMapping("/updateClub")
    public String updateClub(Club club) {
        System.out.println("=======updateClub========");
        System.out.println("club = " + club);
//        执行修改
        clubService.updateClub(club);
//        return "clubBackground";
        return "redirect:/club/getAllClub";
    }

    @RequestMapping("/addClub")
    public String addClub(Club club) {
        System.out.println("=======addClub========");
        System.out.println("club = " + club);
//        执行添加
        clubService.addClub(club);
        return "redirect:/club/clubHome";
    }

    //    加入社团
    @RequestMapping("/clubAddUser")
    public String clubAddUser(Integer clubID, Integer id) {
        System.out.println("=======clubAddUser========");
        System.out.println("clubID = " + clubID + ", id = " + id);
//        执行添加
        clubService.clubAddUser(clubID, id);
        return "redirect:/club/clubHome";
    }

    //    验证用户名是否存在
    @RequestMapping("/checkName")
    @ResponseBody
    public boolean checkName(String name) {
        System.out.println("=====checkName=========");
        System.out.println("name = " + name);
//        根据用户名查询指定的用户信息
        return clubService.checkName(name);
    }


}
